<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <link rel="shortcut icon" href="img/logo.ico" type="image/x-icon">
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpeg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span style="width: 74%;"></span>
        <a id="myblog" href="myblog_list.html">我的主页</a>
        <a id="add" href="blog_add.html">写博客</a>
        <a id="logout" href="javascript: logout();">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <div class="container-right1">
            <div id="alist">
                
            </div>
            <div id="pageDiv" style="text-align:right;padding:20px 50px;">
                <a id="last" href="javascript:lastPage();">上一页</a>
                <a id="next" href="javascript:nextPage();">下一页</a>
            </div>
        </div>
    </div>
    <script>
        // 获取当前url中某个参数
        function getURLparam(key) {
            var param = location.search;
            if (param.length > 0) {
                param = param.substring(1);
                var paramArr = param.split('&');
                for (var i = 0; i < paramArr.length; i++) {
                    var nameVal = paramArr[i].split('=');
                    if (nameVal[0] == key) {
                        return nameVal[1];
                    }
                }
            } else {
                return "";
            }
        }

        var currPage = getURLparam("cp") == "" ? 1 : getURLparam("cp"); // 当前页面
        var pageSize = getURLparam("ps") == "" ? 5 : getURLparam("ps"); // 每页显示的最大条数

        // 上一页
        function lastPage() {
            if (currPage > 1) {
                currPage--;
            }
            location.href = "index.html?cp=" + currPage + "&ps=" + pageSize;
        }

        // 下一页
        function nextPage() {
            currPage++;
            location.href = "index.html?cp=" + currPage + "&ps=" + pageSize;
        }

        // 是否为最后一页
        var flag = false;
        // 请求后端查询列表
        function loadList() {
            jQuery.ajax({
                url: "artlist",
                type: "GET",
                async: false,   // 同步
                data: {
                    "cp": currPage,
                    "ps": pageSize
                },
                success: function (data) {
                    if (data != null && data.data != null) {
                        var artListHtml = "";
                        if (data.data.length < 5) {
                            flag = true;
                        }
                        for (var i = 0; i < data.data.length; i++) {
                            var article = data.data[i];
                            artListHtml += '<div class="blog"><div class="title">' +
                                article.title + '</div><div class="date"><span>日期：' +
                                article.update_time + '</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>作者：' +
                                getAuthor(article.uid) + '</span></div><div class="desc">' +
                                article.desc + '</div><a href="blog_content.html?id=' +
                                article.aid + '" class="detail">查看全文 &gt;&gt;</a></div>'
                        }
                        jQuery("#alist").html(artListHtml);
                    } else {
                        jQuery("#alist").html('<h2 style="text-align: center; color: grey; margin-top: 30%;">无文章</h2>');
                        jQuery("#pageDiv").hide();
                    }
                }
            });
        }
        loadList();

        // 是否是最后一页或者第一页
        if (currPage == 1) {
            jQuery("#last").hide();
        }
        if (flag == true) {
            jQuery("#next").hide();
        }

        // 通过uid获取作者姓名
        function getAuthor(uid) {
            var name = "";
            jQuery.ajax({
                url: "getauthor",
                type: "GET",
                async: false,   // 同步
                data: {
                    "uid": uid
                },
                success: function (data) {
                    if (data != null && data.data != null) {
                        name = data.data.nickname;
                    }
                }
            });
            return name;
        }

        // 注销
        function logout() {
            if (confirm("确认注销？")) {
                // session 清空
                jQuery.ajax({
                    url: "logout",
                    type: "POST",
                    success: function (data) {
                        if (data != null && data.data == 1) {
                            // 注销成功
                            location.href = "login.html";
                        } else {
                            alert("注销失败！");
                        }
                    }
                });
            }
        }

        // 判断是否为登录状态
        function isLogin() {
            jQuery.ajax({
                url: "loginuserinfo",
                type: "GET",
                success: function (data) {
                    if (data != null && data.data == null) {
                        // 未登录
                        jQuery("#myblog").attr("href", "index.html");
                        jQuery("#myblog").text("首页");
                        jQuery("#add").attr("href", "login.html");
                        jQuery("#add").text("登录");
                        jQuery("#logout").attr("href", "reg.html");
                        jQuery("#logout").text("注册");
                    }
                }
            })
        }
        isLogin();
    </script>
</body>

</html>